/**
 * 全部进件控制器
 */
import '../index.less'
import $_fn from '~plugins'
export default ['$scope','$rootScope','$location','$ui','http','$state',function($scope,$rootScope,$location,$ui,http,$state) {
    $scope.$_fn = $_fn
    var search = $location.search()
    $scope.option = {}
    $scope.err = {}
    $scope.state = search.state || 1
    $scope.search = !$_fn.isEmptyObject(search)?search:{state:1}
    $scope.params = $location.search()
    $scope.dataList = []
    $scope.isData = false
    //分页参数
    $scope.page = {
      current: search.pageNum ||  1
    }
    //获取数据的入参
    $scope.params.s_id = '2'
    $scope.params.sIds = search.state || 1
    if(search.state>4){
      $scope.params.s_id = '5'
    }
    $scope.init = function(){
        http.post('selectOrderListInit.do',{},function (data) {
            //申请期限
            data.data.applyTermList.unshift({id:'', at_name:'选择期限'})
            $scope.params.at_id = ''
            $scope.option.period = data.data.applyTermList
        })
    }
    $scope.init()
    //获取数据的函数
    $scope.getDada = function(page,params){
        console.log('getDada', $scope.params)
        params.pageNum = page
        http.post('selectOrderList.do',params,function (data) {
            var _data = data.data
            $scope.page.pageNum =  $scope.page.current
            $scope.page.pageSize = _data.pageSize
            $scope.page.total = _data.total
            if(data.errcode > 0){
                //列表无数据
                $scope.$apply(function () {
                    if(_data.list.length == 0){
                        $scope.isData = true
                        $scope.dataList = []
                    }else {
                        $scope.isData = false
                        $scope.dataList = _data.list
                    }
                })
            }
        },function(){
            //报错时
            $scope.$apply(function () {
                $scope.isData = true
                $scope.dataList = []
            })
        })
    }
    //提示错误
    $scope.errorToast = function(text) {
        $ui.toast({text:text,type:'warning',position:'center',appendTo:'.main-content'});
    }
    //初始化数据
    $scope.getDada(1,$scope.params)
    /**
     * 四级权限菜单
     * state = 1                      地址栏所要显示的菜单
     * path = link@、alert@           link(连接)、alert(弹窗) 例:link@application.intoa(跳转到全部进件列表)
     * */
    $scope.operation = function (path,item) {
        var _path = path.split('@')
        console.log('_path:', _path,item)
        let params = JSON.stringify(item)
        if(_path[0] == 'link'){
            console.log(path)
            $state.go(_path[1],item,{reload:true})
        }
        if(_path[0] == 'alert'){
          $ui.modal({content:"<"+_path[1]+" params="+params+" ></"+_path[1]+">"})
      }
    }
    
    //搜索
    $scope.searchHandle = function () {
      if($scope.params.keyword && $scope.params.type==-1){
        $scope.err.type = true
        $scope.errorToast('左侧字段没有选择')
        return
      }
      $location.search($scope.params)
      $scope.getDada(1,$scope.params)
    }

    //获取当前路由的四级权限
    var menu = $_fn.getLocal('parentMenu')
    $scope.subMenu = []
    var _subMenu = function (path,_menu) {
        for (var a in _menu) {
            if (_menu[a].children) {
                for (var i in _menu[a].children) {
                    if (_menu[a].children[i].path == path) {
                        $scope.subMenu = _menu[a].children[i].children
                        break
                    } else {
                        _subMenu(path,_menu[a].children[i].children)
                    }
                }
            }
        }
    }
    _subMenu('/application/intoa',menu.children)
}]
